package net;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UdpEchoClient {
    private DatagramSocket socket=null;
    private String serverIp;
    private int serverPort;
    public UdpEchoClient(String serverIp, int serverPort) throws SocketException {
        this.serverIp=serverIp;
        this.serverPort=serverPort;
        socket=new DatagramSocket();
    }
    public void start() throws IOException {
        Scanner scanner=new Scanner(System.in);
        while (true){
            if (!scanner.hasNext()){
                break;
            }
            String requst=scanner.next();
            DatagramPacket resqustPacket=new DatagramPacket(requst.getBytes(),requst.getBytes().length,
                    InetAddress.getByName(serverIp),serverPort);
            socket.send(resqustPacket);

            DatagramPacket responsePacket=new DatagramPacket(new byte[4399],4399);
            socket.receive(responsePacket);
            String response=new String(responsePacket.getData(),0,responsePacket.getLength());
            System.out.println(response);
        }
    }

    public static void main(String[] args) throws IOException {
       UdpEchoClient client=new UdpEchoClient("127.0.0.1",9090);
       client.start();
    }
}
